<?php

namespace app\api\controller\xiservice;

use app\common\controller\xiservice\XiserviceApi;
use addons\xiservice\controller\WeixinMini;
use app\common\model\xiservice\Wxtemplate;
use app\common\model\xiservice\Refuse;
use app\common\model\xiservice\User as XiserviceUser;
use app\common\model\xiservice\Benfit as BenfitModel;

/**
 * 定时任务
 */
class Time extends XiserviceApi
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 标记异常订单
     * 下单多少分钟 没人接单则标记异常订单 后台处理
     */
    public function markorder()
    {
        $model = new \app\common\model\xiservice\Order;
        $minutes = isset(config('xiservice.order')['auto_mark_minutes']) ? config('xiservice.order')['auto_mark_minutes'] : 30;
        $expiretime = time() - $minutes * 60;
        $list = $model->where('order_status', '20')->where('unusual', 0)->where('createtime', '<', $expiretime)->select();
        foreach ($list as $key => $value) {
            $res = $model->where('id', $value['id'])->update(['unusual' => 1]);
        }
        $this->success('标记异常订单成功', []);
    }

    //超时提醒发送
    public function remindorder()
    {
        $model = new \app\common\model\xiservice\Order;
        $msgmodel = new \app\common\model\xiservice\Message;
        $minutes = 30;
        $expiretime = time() - $minutes * 60;
        $list = $model->where('order_status', '30')->where('send_remind', 0)->where('service_start_time', '<', $expiretime)->select();
        foreach ($list as $key => $value) {
            $workermsg = $msgmodel->sendOrderMessage($value, 5);
            $usermsg = $msgmodel->sendRemindUserMessage($value);
            $res = $model->where('id', $value['id'])->update(['send_remind' => 1]);
        }
        $this->success('超时提醒发送成功', []);
    }

    /**
     * 取消未支付订单
     * 下单多少分钟 未支付 则取消订单
     */
    public function cancelorder()
    {
        $model = new \app\common\model\xiservice\Order;
        $minutes = isset(config('xiservice.order')['auto_close_minutes']) ? config('xiservice.order')['auto_close_minutes'] : 30;
        $expiretime = time() - $minutes * 60;
        $list = $model->where('order_status', '10')->where('createtime', '<', $expiretime)->select();
        foreach ($list as $key => $value) {
            $res = $model->changeOrderStatus($value['id'], 'cancel');
        }
        $this->success('取消未支付订单成功', $list);
    }

    /**
     * 取消订单
     * 下单多少分钟 已支付 服务人员未接单 则取消订单 =>超时处理
     */
    public function cancelconfirmorder()
    {
        $wx = new WeixinMini();
        $temp = new Wxtemplate();
        $model = new \app\common\model\xiservice\Order;
        $minutes = isset(config('xiservice.order')['auto_close_minutes']) ? config('xiservice.order')['auto_close_minutes'] : 30;
        $expiretime = time() - $minutes * 60;
        $list = $model->where('order_status', '20')->where('chose_type', 1)->where('pay_status', '20')->where('createtime', '<', $expiretime)->select();
        foreach ($list as $key => $value) {
            $res = $model->changeOrderStatus($value['id'], 'cancel');

            $order = $model->where('id', $value['id'])->find();
            //通知用户
            $openid = XiserviceUser::where('user_id', $order['user_id'])->value('mini_openid');
            $message = $temp->messageData($order, $openid, 'order_cancel');
            if ($message) {
                $result = $wx->subscribeMessage($message);
            }
            //超时处理 
            $refuse = Refuse::insertGetId(['user_id' => $order['worker_id'], 'order_id' => $value['id'], 'createtime' => time()]);
        }
        $this->success('取消订单成功', $list);
    }


    /**
     * 服务中的订单 24小时后自动进入质保
     */
    public function qualityorder()
    {
        $model = new \app\common\model\xiservice\Order;
        $day = isset(config('xiservice.order')['auto_receipt_days']) ? config('xiservice.order')['auto_receipt_days'] : 1;
        $expiretime = time() - $day * 24 * 60 * 60;
        $list = $model->where('order_status', '40')->where('pay_status', '20')->where('service_start_time', '<', $expiretime)->select();
        foreach ($list as $key => $value) {
//            $res = $model->changeOrderStatus($value['id'],'finished');
            $res = $model->changeOrderStatus($value['id'], 'quality');
        }
        $this->success('订单确认进入质保成功', $list);
    }

    /**
     * 质保到期转完成订单
     */
    public function finishorder()
    {
        $model = new \app\common\model\xiservice\Order;
        $list = $model->where('order_status', '70')->where('quality_end_time', '<', time())->select();
        foreach ($list as $key => $value) {
            $res = $model->changeOrderStatus($value['id'], 'finished');
        }
        $this->success('质保到期订单自动完成成功', $list);
    }

    /**
     * 解冻
     */
    public function unfreeze()
    {
        $benfitmodel = new \app\common\model\xiservice\Benfit;
        $day = isset(config('xiservice.order')['auto_freeze_days']) ? config('xiservice.order')['auto_freeze_days'] : 1;
        $expiretime = time() - $day * 24 * 60 * 60;
        $list = $benfitmodel->where('type', 1)->where('status', 1)->where('createtime', '<', $expiretime)->select();
        foreach ($list as $key => $value) {
            $res = $benfitmodel->unfreeze($value);
        }
        $this->success('资金解冻成功', $list);

    }


}